<?php

namespace app\api\controller\xykeep;

use addons\xykeep\exception\Exception;
use app\api\model\xykeep\Course as CourseModel;
use app\api\model\xykeep\User;
use app\common\controller\Api;
use app\api\model\xykeep\course\Order as CourseOrderModel;
use think\Db;


/**
 * XYkeep 课程订单接口
 */
class CourseOrder extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    

    /**
	 * 列表
	 */
	public function lists()
    {
    	$params = $this->request->post();
        $user_id = $this->auth->id;
        $where = [
          'user_id' => $user_id,
        ];
        if(isset($params['status']) && !($params['status'] === '')){
            $where['status'] = $params['status'];
        }
        $data = CourseOrderModel::where($where)->order('id desc')->paginate();
        $this->success('课程订单列表', $data);
    }

	public function add()
    {
        $params = $this->request->post();
        $order = CourseOrderModel::addOrder($params);
        $this->success('课程订单添加成功', $order);
    }

    /**
     * 查询订单金额
     * @params['buy_num'] 购买数量
     * @params['course_id'] 课程
     * @return $total_money String
     */
    public function countPrice()
    {
        $params = $this->request->post();
        $course_info = CourseModel::where('id', $params['course_id'])->find();
        $cout = CourseOrderModel::count_price($params['buy_num'],$course_info['price']);
        $this->success('查询金额',$cout);
    }


    public function detail()
    {
        $params = $this->request->post();
        $CourseOrderModel = new CourseOrderModel();
        $info = $CourseOrderModel->with('course')->where('id',$params['id'])->find();
        $this->success('课程订单详情', $info);
    }
	
	public function cancel()
    {
        $params = $this->request->post();
        $this->success('取消成功', CourseOrderModel::cancelOrder($params));
    }

    /**
     * 获取订单状态
     */
    public function getStatus()
    {
        $params = $this->request->post();
        $status = CourseOrderModel::where('id',$params['order_id'])->value('status');
        $this->success('获取订单状态',$status);
    }

}